package project01;

import java.util.Scanner;

//工具类
public class Utility {

    private static Scanner scan = new Scanner(System.in);

    //用于界面菜单的选择,该方法读取键盘,用户输入'1'~'4'的任意字符,则方法返回
    public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1);
            c = str.charAt(0);
            if (c != '1' && c != '2' && c != '3' && c != '4') {
                System.out.print("选择错误,请重新输入:");
            } else {
                break;
            }
        }
        return c;
    }

    //用于收入和输出金额的输入
    public static int readNumber() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(4);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入的数字有误,请重新输入:");
                ;
            }
        }
        return n;
    }

    //用于收入和支出说明的输入
    public static String readString() {
        String str = readKeyBoard(8);
        return str;
    }

    //用于确认选择的输入
    public static char readConfirmSelecton() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");

            }
        }
        return c;
    }

    public static String readKeyBoard(int limit) {
        String line = "";
        while (true) {
            line = scan.nextLine();
            if (line.length() < 1 || line.length() > limit){
                System.out.print("输入长度(不大于"+limit+")错误,请重新输入:");
                continue;
            }else {
                break;
            }
        }
        return line;
    }

}
